/*!
 * Ext JS Library 3.0.0
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */



Ext.Desktop = function(app){

	this.taskbar = new Ext.ux.TaskBar(app);
	var taskbar = this.taskbar;
	
	var desktopEl = Ext.get('x-desktop');
    var taskbarEl = Ext.get('ux-taskbar');
    var shortcuts = Ext.get('x-shortcuts');
	
	desktopEl.on("contextmenu",function(e){
		e.stopEvent();
		new Ext.menu.Menu({
			id:"desk_ctx_menu",
			shadow:false,
			items:[{text:c.ArrangeIcons,handler:function(){}},{text:c.Refresh,iconCls:'refresh'},'-',{text:c.Properties}]
		}).showAt(e.getXY());
		
	},window,{stopEvent :true,buffer:100});


    var windows = new Ext.WindowGroup();
    var activeWindow;
		
    function minimizeWin(win){
        win.minimized = true;
        win.hide();
    }

	function markActive(win){
        if(activeWindow && activeWindow != win){
            markInactive(activeWindow);
        }
        taskbar.setActiveButton(win.taskButton);
        activeWindow = win;
        Ext.fly(win.taskButton.el).addClass('active-win');
        win.minimized = false;
    }

    function markInactive(win){
        if(win == activeWindow){
            activeWindow = null;
            Ext.fly(win.taskButton.el).removeClass('active-win');
        }
    }

    function removeWin(win){
    	taskbar.removeTaskButton(win.taskButton);
        layout();
    }

    function layout(){
        desktopEl.setHeight(Ext.lib.Dom.getViewHeight()-taskbarEl.getHeight());
    }
    Ext.EventManager.onWindowResize(layout);

    this.layout = layout;
	
	this.createNoBWin = function(config,cls){
		var win = new (cls||Ext.Window)(
			Ext.applyIf(config||{}, {
				minimizable: true,
                //maximizable: true,
				closable:true,
				resizable: false,
				modal: 'true',
				layout:'fit',
				plain: true
			})
		);
		win.on('hide',function(){
			win.destroy();
		}.createDelegate(this));
		win.show();
	};

    this.createWindow = function(config, cls){
    	var win = new (cls||Ext.Window)(
            Ext.applyIf(config||{}, {
                manager: windows,//定义窗口分组（属于哪一组）
                minimizable: true,
                maximizable: true
            })
        );
        win.render(desktopEl);
        win.taskButton = taskbar.addTaskButton(win);

        win.cmenu = new Ext.menu.Menu({
            items: [

            ]
        });

        win.animateTarget = win.taskButton.el;
        
        win.on({
        	'activate': {
        		fn: markActive
        	},
        	'beforeshow': {
        		fn: markActive
        	},
        	'deactivate': {
        		fn: markInactive
        	},
        	'minimize': {
				fn: minimizeWin
			},
			'close': {
				fn: removeWin
			}
		});

		layout();
		return win;
	};

	this.getManager = function(){
		return windows;
	};

	this.getWindow = function(id){
		return windows.get(id);
	}

	this.getWinWidth = function(){
		var width = Ext.lib.Dom.getViewWidth();
		return width < 200 ? 200 : width;
	}
		
	this.getWinHeight = function(){
		var height = (Ext.lib.Dom.getViewHeight()-taskbarEl.getHeight());
		return height < 100 ? 100 : height;
	}
		
	this.getWinX = function(width){
		return (Ext.lib.Dom.getViewWidth() - width) / 2
	}
		
	this.getWinY = function(height){
		return (Ext.lib.Dom.getViewHeight()-taskbarEl.getHeight() - height) / 2;
	}

	layout();

	if(shortcuts){//快捷方式
		shortcuts.on('click', function(e, t){
			if(t = e.getTarget('dt', shortcuts)){
				e.stopEvent();
				if(t.id != "website_win-shortcut") {
					alert("弘迈科技 - 警告","你的权限不足，请与管理员联系！");
					return;
				}
				var module = app.getModule(t.id.replace('-shortcut', ''));
                if(module){
					
					var desktop = MyDesktop.getDesktop();
					var win = desktop.getWindow(module.windowId);
                    if(!win){
						win = desktop.createWindow({
							id: module.windowId,
							title:module.text,
							width:910,
							height:500,
							items:module.item,
							/*	
							new Ext.Panel({
								id:'load_site',
								border:false,
								layout:'fit',
								html:'<iframe src="'+module.url+'" marginheight="0" marginwidth="0" scrolling="auto" frameborder="0" width="100%" height="100%"></iframe>'
							})*/
							//html : '<p>Something useful would be in here.</p>',
							iconCls: module.iconCls,
							layout: 'fit',
							shim:false,
							animCollapse:false,
							constrainHeader:true
						});
					}
					win.show();
				}else{
				}
				
				/*
				var id = t.id.replace('-shortcut', '');
				var module = shortcut_modules[id];
				if(module){
					//var pw = new PermitWindow({modal:true, closable:true,url:module.url, title: module.title, redirect: module.redirect});
					//pw.showLogin();
					//alert(module);
					//module.handler(module);
					//module.createWindow();
					this.createWindow();
				}*/
			}
		});

		shortcuts.on('contextmenu',function(e){//声明菜单类型
			e.stopEvent();//这行是必须的
			new Ext.menu.Menu({
				id:"dt_ctx_menu",
				shadow:false,
				items:[{text:'打开',handler:function(){}},{text:'删除',iconCls:'remove'},'-',{text:'重命名'},{text:'属性'}]
			}).showAt(e.getXY());
			//rightClick.showAt(e.getXY());//取得鼠标点击坐标，展示菜单
		}); 
	}
};

